﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using Shell32;

namespace MainSite.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

        [NonAction]
        private static string UnzipFile(string inputFileName, string destinationPath)
        {
            Shell shell = new Shell();
            
            Folder sourceFolder = shell.NameSpace(inputFileName);
            Folder destinationFolder = shell.NameSpace(destinationPath);
            string outputFileName = sourceFolder.Items().Item(0).Name;
            
            destinationFolder.CopyHere(sourceFolder.Items(), 16);

            return outputFileName;
        } 

        [HttpPost]
        public ActionResult FileUpload()
        {
            HttpPostedFileBase viewsFile = Request.Files["Views"];
            string viewsFileName = Server.MapPath("~/Uploads/") + Path.GetFileName(viewsFile.FileName);

            viewsFile.SaveAs(viewsFileName);

            UnzipFile(viewsFileName, Server.MapPath("~/Views/"));

            HttpPostedFileBase controllersFile = Request.Files["Controllers"];
            string controllersFileName = Server.MapPath("~/Bin/") + Path.GetFileName(controllersFile.FileName);

            controllersFile.SaveAs(controllersFileName);

            return RedirectToAction("Index");
        }

    }
}
